package com.example.serviceDriverUser.service;

import com.example.internalcommon.dto.Car;
import com.example.internalcommon.dto.ResponseResult;
import com.example.internalcommon.response.TerminalResponse;
import com.example.internalcommon.response.TrackResponse;
import com.example.serviceDriverUser.mapper.CarMapper;
import com.example.serviceDriverUser.remote.ServiceMapClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.List;

/**
 * @Author lyra
 * @Date 2023/4/12 21:34
 **/
@Service
public class CarService {
    @Autowired
    private CarMapper carMapper;

    @Autowired
    private ServiceMapClient serviceMapClient;

    public ResponseResult<TerminalResponse> addCar(Car car){
        LocalDateTime now = LocalDateTime.now();
        car.setGmtModified(now);
        car.setGmtCreate(now);
        // 保存车辆
        carMapper.insert(car);

        // (1)获得此车辆对应的tid
        ResponseResult<TerminalResponse> responseResult = serviceMapClient.addTerminal(car.getVehicleNo(),car.getId() + "");
        String tid = responseResult.getData().getTid();
        car.setTid(tid);

        // (2)获得此车辆的轨迹id: trid
        ResponseResult<TrackResponse> terminalResponseResponseResult = serviceMapClient.addTrack(car.getTid());
        String trid = terminalResponseResponseResult.getData().getTrid();
        String trname = terminalResponseResponseResult.getData().getTrname();

        car.setTrid(trid);
        car.setTrname(trname);

        carMapper.updateById(car);
        return ResponseResult.success("");
    }

    public ResponseResult<Car> getCarById(Long id){
        HashMap<String, Object> map = new HashMap<>();
        map.put("id", id);
        List<Car> cars = carMapper.selectByMap(map);
        return ResponseResult.success(cars.get(0));
    }

}
